package cn.edu.gsau.info.service.impl;

import cn.edu.gsau.info.dao.MenuMapper;
import cn.edu.gsau.info.dao.UserDao;
import cn.edu.gsau.info.dao.UserDao;
import cn.edu.gsau.info.models.LoginUser;
import cn.edu.gsau.info.models.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.code.kaptcha.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    UserDao userDao;

    //1：注入MenuMapper对象
    @Autowired
    MenuMapper menuMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("userName",username);
       /* qw.eq(username!=null && username.equals("")==false,"username",101);*/
        User user = userDao.selectOne(qw);

        if(Objects.isNull(user)){
            throw new RuntimeException("错误的用户名");
        }



        //2: 查询用户的权限
        List<String> permissions = menuMapper.selectPermsByUserId(user.getId());


        return new LoginUser(user,permissions);
    }
}
